package com.atguigu.jxc.util;

import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.User;

import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author tongatong
 * @name UserUtil
 * @description
 * @date 2021/10/15
 * @time 23:30
 */
public class UserUtil {
    public static <T> T setUserInfoForObject(HttpSession session, T t) {
        User user = (User)session.getAttribute("currentUser");
        Method setTrueName;
        Method setUserId;
        Class clazz = t.getClass();
        try {
            setTrueName = clazz.getMethod("setTrueName", String.class);
            setUserId = clazz.getMethod("setUserId", Integer.class);
            setTrueName.invoke(t, user.getTrueName());
            setUserId.invoke(t, user.getUserId());
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            return null;
        }

        return t;
    }

}
